package org.appfuse.model;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.compass.annotations.SearchableProperty;

import com.otp.model.test.template.AttributeSet;
import com.otp.model.test.template.TestTemplate;

/**
 * This class is used to represent an address with address,
 * city, province and postal-code information.
 *
 * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
 */
@Entity
@Table(name = "organizations")
public class Organization extends BaseObject implements Serializable {


	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Long objectId;
	String organizationName;
	String organizationDescription;
	AttributeSet attSet;
	Set<TestTemplate> testTemplates;


	public Organization(){};
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "object_id")
	public Long getObjectId() {
		return objectId;
	}
	public void setObjectId(Long objectId) {
		this.objectId = objectId;
	}
	
	@Column(name="organization_name",nullable = false, length = 100, unique = true)
	@SearchableProperty
	public String getOrganizationName() {
		return organizationName;
	}

	public void setOrganizationName(String organizationName) {
		this.organizationName = organizationName;
	}

	@Column(name="organization_description",length = 1000)
	@SearchableProperty
	public String getOrganizationDescription() {
		return organizationDescription;
	}

	public void setOrganizationDescription(String organizationDescription) {
		this.organizationDescription = organizationDescription;
	}

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "fk2demo_set")
	public AttributeSet getAttSet() {
		return attSet;
	}
	public void setAttSet(AttributeSet attSet) {
		this.attSet = attSet;
	}
	
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(
            name = "template_org",
            joinColumns = {@JoinColumn(name = "fk2organization")},
            inverseJoinColumns = @JoinColumn(name = "fk2test_template")
    )
    public Set<TestTemplate> getTestTemplates() {
        return testTemplates;
    }
    
    public void setTestTemplates(Set<TestTemplate> testTemplates){
    	this.testTemplates = testTemplates;
    }


	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (!(o instanceof Organization)) {
			return false;
		}

		final Organization address1 = (Organization) o;

		return this.hashCode() == address1.hashCode();
	}


	/**
	 * Overridden hashCode method - compares on address, city, province, country and postal code.
	 * @return hashCode
	 */
	public int hashCode() {
		int result;
		result = (organizationName != null ? organizationName.hashCode() : 0);
		return result;
	}

	/**
	 * Returns a multi-line String with key=value pairs.
	 * @return a String representation of this class.
	 */
	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
		.append("organizationName", this.organizationName).toString();
	}
}
